package Common;

import java.io.*;

import storage.data.PageID;

import Transaction.Transaction;


public class Convert {

	/**
	 * read 4 bytes from given array at the specified position convert it to an
	 * integer.
	 */

	public static int getIntValue(int position, byte data[],Transaction tx,String tablename)
			throws Exception {
		/*InputStream in;
		DataInputStream instr;
		int value;
		byte tmp[] = new byte[4];

		// copy the value from data array out to a tmp byte array
		System.arraycopy(data, position, tmp, 0, 4);

		/*
		 * creates a new data input stream to read data from the specified input
		 * stream
		 */
		/*in = new ByteArrayInputStream(tmp);
		instr = new DataInputStream(in);
		value = instr.readInt();*/

		return tx.getInt(tablename, data, position);
		
		
	}

	/**
	 * read 4 bytes from given byte array and convert it to a float.
	 * 
	 * @param
	 * @return
	 * 
	 */

	public static float getFloValue(int position, byte data[],Transaction tx,String tableName)
			throws Exception {
//		InputStream in;
//		DataInputStream instr;
//		float value;
//		byte tmp[] = new byte[4];
//
//		// copy the value from data array out to a tmp byte array
//		System.arraycopy(data, position, tmp, 0, 4);
//
//		/*
//		 * creates a new data input stream to read data from the specified input
//		 * stream
//		 */
//		in = new ByteArrayInputStream(tmp);
//		instr = new DataInputStream(in);
//		value = instr.readFloat();

		
		return tx.getFloat(tableName, data, position);

	}

	/**
	 * read 2 bytes from given byte array and convert it to a short integer.
	 * 
	 * @param position
	 * @param data
	 * @return
	 * @throws java.io.IOException
	 */
	public static short getShortValue(int position, byte[] data,Transaction tx,String table)
			throws Exception {
//		InputStream in;
//		DataInputStream instr;
//		short value;
//		byte tmp[] = new byte[2];
//
//		// copy the value from data array out to a tmp byte array
//		System.arraycopy(data, position, tmp, 0, 2);
//
//		/*
//		 * creates a new data input stream to read data from the specified input
//		 * stream
//		 */
//		in = new ByteArrayInputStream(tmp);
//		instr = new DataInputStream(in);
//		value = instr.readShort();

		return tx.getShort(table, data, position);
	}

	/**
	 * reads a string that has been encoded using a modified UTF-8 format from
	 * the given byte array at the specified position
	 * 
	 * @param position
	 * @param data
	 * @param length
	 * @return
	 * @throws java.io.IOException
	 */

	public static String getStrValue(int position, byte data[],Transaction tx,String table)
			throws Exception {
//		InputStream in;
//		DataInputStream instr;
//
//		String value;
//		byte tmp[] = new byte[length];
//
//		System.arraycopy(data, position, tmp, 0, length);
//
//		in = new ByteArrayInputStream(tmp);
//		instr = new DataInputStream(in);
//		value = instr.readUTF();
		return tx.getString(table, data, position);
	}

	/**
	 * 
	 * @param position
	 * @param data
	 * @return
	 * @throws java.io.IOException
	 */
	public static char getCharValue(int position, byte[] data,Transaction tx,String table)
			throws Exception {
//		InputStream in;
//		DataInputStream instr;
//		char value;
//		byte tmp[] = new byte[2];
//		// copy the value from data array out to a tmp byte array
//		System.arraycopy(data, position, tmp, 0, 2);
//
//		/*
//		 * creates a new data input stream to read data from the specified input
//		 * stream
//		 */
//		in = new ByteArrayInputStream(tmp);
//		instr = new DataInputStream(in);
//		value = instr.readChar();
		return tx.getChar(table, data, position);
	}

	/**
	 * 
	 * @param value
	 * @param position
	 * @param data
	 * @throws java.io.IOException
	 */
	public static void setIntValue(int value, int position, byte[] data,Transaction tx,String table,PageID pid)
			throws Exception {
		/*
		 * creates a new data output stream to write data to underlying output
		 * stream
		 */

//		OutputStream out = new ByteArrayOutputStream();
//		DataOutputStream outstr = new DataOutputStream(out);
//
//		// write the value to the output stream
//		outstr.writeInt(value);
//
//		byte[] tmp = ((ByteArrayOutputStream) out).toByteArray();
//		System.arraycopy(tmp, 0, data, position, 4);
		tx.set(table, pid, data, position, value);
	}

	public static void setFloValue(float value, int position, byte[] data,Transaction tx,String table,PageID pid)
			throws Exception {
//		OutputStream out = new ByteArrayOutputStream();
//		DataOutputStream outstr = new DataOutputStream(out);
//
//		outstr.writeFloat(value);
//
//		// creates a byte array with this output stream size and the
//		// valid contents of the buffer have been copied into it
//		byte[] tmp = ((ByteArrayOutputStream) out).toByteArray();
//
//		// copies the first 4 bytes of this byte array into data[]
//		System.arraycopy(tmp, 0, data, position, 4);
		tx.set(table, pid, data, position, value);
	}

	public static void setShortValue(short value, int position, byte data[],Transaction tx,String table,PageID pid)
			throws Exception {

//		OutputStream out = new ByteArrayOutputStream();
//		DataOutputStream outstr = new DataOutputStream(out);
//
//		outstr.writeShort(value);
//
//		// creates a byte array with this output stream size and the
//		// valid contents of the buffer have been copied into it
//		byte[] tmp = ((ByteArrayOutputStream) out).toByteArray();
//
//		// copies the first 2 bytes of this byte array into data[]
//		System.arraycopy(tmp, 0, data, position, 2);
		tx.set(table, pid, data, position, value);
	}

	public static void setStrValue(String value, int position, byte[] data,Transaction tx,String table,PageID pid)
			throws Exception {
		
//		OutputStream out = new ByteArrayOutputStream();
//		DataOutputStream outstr = new DataOutputStream(out);
//
//		// write the value to the output stream
//
//		outstr.writeUTF(value);
//		
//		byte[] tmp = ((ByteArrayOutputStream) out).toByteArray();
//
//		int sz = outstr.size();
//		
//		System.arraycopy(tmp, 0, data, position, sz);
		tx.set(table, pid, data, position, value);
	}
	
	public static void setCharValue (char value, int position, byte []data,Transaction tx,String table,PageID pid)
    throws Exception
    {      
//      OutputStream out = new ByteArrayOutputStream();
//      DataOutputStream outstr = new DataOutputStream (out);
//      
//      outstr.writeChar(value);  
//      
//      byte []tmp = ((ByteArrayOutputStream) out).toByteArray();
//      
//      System.arraycopy (tmp, 0, data, position, 2);
		tx.set(table, pid, data, position, value);
      
    }
	
	
	
	
	
	public static void SsetIntValue(int value, int position, byte[] data)
	throws Exception {
		ByteArray.writeInt(data, position, value);
	}

	public static void SsetFloValue(float value, int position, byte[] data)
	throws Exception {
		ByteArray.writeFloat(data, position,value);
	}

	public static void SsetShortValue(short value, int position, byte data[],Transaction tx,String table,PageID pid)
	throws Exception {
		tx.set(table, pid, data, position, value);
	}

	public static void SsetStrValue(String value, int position, byte[] data)
	throws Exception {
		ByteArray.writeString(data, position,value);
	}

	public static void SsetCharValue (char value, int position, byte []data,Transaction tx,String table,PageID pid)
	throws Exception
	{      
		tx.set(table, pid, data, position, value);

	}	
	
}
